
#include "ftp_client.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/unistd.h>

/*
* @brief 连接fpt服务器
* @param 无
* @return -1/成功建立的套接字
*/
static int ftp_connect(FTP_SERVER_INFO_T* pFtpServer)
{
	int iRet = -1;
	int iClientSock;
	struct sockaddr_in seraddr;
	
	//1.建立套接字
	iClientSock = socket(AF_INET,SOCK_STREAM,0);
	if(iClientSock < 0)
	{
		printf("sock error\n");
		return -1;
	}

	//2.绑定通信地址
	seraddr.sin_family = AF_INET;
	seraddr.sin_port = htons(pFtpServer->serverPort);
	seraddr.sin_addr.s_addr = inet_addr(pFtpServer->szServerIP);
	iRet = connect(iClientSock,(struct sockaddr*)&seraddr,sizeof(seraddr));
	if(iRet < 0)
	{
		printf("connect error\n");
		return -1;
	}
	
	return iClientSock;
}

/*
* @brief 等待服务器发送数据
* @param 
* @param
* @len
* @rturn
*/
static int ftp_recv(int iCtrlSock,char *buf, int len)
{
	int iRet = -1;
	if(NULL == buf)
	{
		printf("buf is empty\n");
		return -1;
	}
	iRet = recv(iCtrlSock,buf,len,0);
	if(iRet < 0)
	{
		printf("recv error\n");
		return -1;
	}
	return iRet;
}

/*
* @brief 向服务器发送数据
* @param 
* @param
* @len
* @rturn
*/
static int ftp_send(int iCtrlSock,char *buf, int len)
{
	int iRet = -1;
	if(NULL == buf)
	{
		printf("buf is empty\n");
		return -1;
	}
	iRet = send(iCtrlSock,buf,len,0);
	if(iRet < 0)
	{
		printf("send error\n");
		return -1;
	}
	return iRet;
}


/*定义全局结构体*/
static FTP_CLIENT_T g_ftpClient =
{
	.name 			 = "ftpclient",
	.pFunFtp_connect = ftp_connect,
	.pFunFtp_recv 	 = ftp_recv,
	.pFunFtp_send    = ftp_send,
};

/*
 * @brief 获取全局结构体参数
*/
FTP_CLIENT_T *ftp_get_client_param(void)
{
	return &g_ftpClient;
}


















